﻿namespace Bootstrap5;

using Abstractions;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;

/// <summary>
/// Represents a button component with '.btn' class.
/// </summary>
[CssClass("btn")]
public class Button : BootstrapChildContentComponentBase, IHasOnClick
{
    /// <summary>
    /// Initialized a new instance of <see cref="Button"/> class.
    /// </summary>
    public Button()
    {

    }

    /// <summary>
    /// The color of button.
    /// </summary>
    [Parameter] public Color? Color { get; set; }

    /// <summary>
    /// The linked style, but <see cref="Color"/> is out of work.
    /// </summary>
    [Parameter] public bool Linked { get; set; }

    /// <summary>
    /// Outline style.
    /// </summary>
    [Parameter] public bool Outline { get; set; }

    /// <summary>
    /// The size.
    /// </summary>
    [Parameter] public Size? Size { get; set; }
    /// <summary>
    /// Disable the button status.
    /// </summary>
    [Parameter] public bool Disabled { get; set; }

    /// <summary>
    /// Represents an active status.
    /// </summary>
    [Parameter] [CssClass("active")] public bool Actived { get; set; }
    /// <summary>
    /// The element of button.
    /// </summary>
    [Parameter] public ButtonElement ElementType { get; set; } = ButtonElement.Button;
    /// <summary>
    /// Support dropdown with <see cref="Bootstrap5.DropDownMenu"/> component.
    /// </summary>
    [Parameter]
    [CssClass("dropdown-toggle")]
    [DataTriggerToggle("dropdown")]
    public bool DropDown { get; set; }
    
    /// <summary>
    /// Set <see cref="DropDown"/> parameter <c>true</c> to split with other buttons.
    /// </summary>
    [Parameter][CssClass("dropdown-toggle-split")] public bool DropDownSplit { get; set; }

    /// <summary>
    /// A callback method when clicking button.
    /// </summary>
    [Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }

    protected override void CreateCssClass(ICssClassBuilder builder)
    {
        if (Linked)
        {
            builder.Add("btn-link");
        }
        else
        {
            if (Color.HasValue)
            {
                builder.Add($"btn{(Outline ? "-outline" : string.Empty)}-{Color.GetEnumCssClass()}");
            }
        }

        builder.Add($"btn-{Size.GetEnumCssClass()}", Size.HasValue);
    }

    protected override void BuildExtraAttributes(RenderTreeBuilder builder, int sequence = 5)
    {
        builder.AddAttribute(sequence, "disabled", Disabled);
    }

    protected override string GetHtmlTagName()
    => ElementType.GetEnumDefaultValue().ToString();
}
